<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary"  v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="detailTypes" @row-dblclick="handledrawer" highlight-current-row v-loading="listLoading" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="160">
			</el-table-column>
			<el-table-column prop="name" label="字典名" width="220" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="字典编号" width="220" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :current-page="currentPage"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增和编辑界面-->
		<el-dialog :title='title' :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="字典名" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="字典编号" prop="sn">
					<el-input v-model="saveForm.sn" auto-complete="off"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">保存</el-button>
			</div>
		</el-dialog>
		<!--抽屉-->
		<el-drawer
				:title='title'
				:visible.sync="drawer"
				direction="rtl"
				size="50%">
			<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
				<el-form :inline="true" :model="filters">
					<el-form-item>
						<el-input v-model="filters.keyword" placeholder="请输入关键字"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary"  v-on:click="search">查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="handleAdd">新增</el-button>
					</el-form-item>
				</el-form>
			</el-col>
			<el-table :data="details" highlight-current-row v-loading="listLoading" style="width: 100%;">
				<el-table-column type="selection" width="55">
				</el-table-column>
				<el-table-column type="index" width="160">
				</el-table-column>
				<el-table-column prop="name" label="明细名" width="220" sortable>
				</el-table-column>
				<el-table-column label="操作" width="150">
					<template scope="scope">
						<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
						<el-button type="danger" size="small" @click="del(scope.$index, scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-col :span="24" class="toolbar">
				<el-pagination layout="prev, pager, next" @current-change="dichandleCurrentChange"
							   :page-size="dicpageSize"
							   :current-page="diccurrentPage"
							   :total="dictotal"
							   style="float:right;">
				</el-pagination>
			</el-col>
		</el-drawer>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
                    keyword: ''
				},
                detailTypes:[],
                details:[],
                total: 0,
                dictotal:0,
                dicpageSize:22,
				diccurrentPage:1,
                currentPage:1,
                pageSize:6,
                title:'',
				listLoading: false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
                drawer: false,
                direction: 'rtl',
				editFormRules: {
					name: [
						{ required: true, message: '请输入字典名', trigger: 'blur' }
					],
					sn: [
						{ required: true, message: '请输入字典编号', trigger: 'blur' }
					]
				},
				saveFormVisible: false,//新增界面是否显示
				saveLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入字典名', trigger: 'blur' }
					],
                    sn: [
                        { required: true, message: '请输入字典编号', trigger: 'blur' }
                    ]
				},
				//新增界面数据
				saveForm: {
                    id:null,
                    name: "",
					sn:""
				}

			}
		},
		methods: {
		    getDetails(row) {
                let para = {
                    currentPage: this.diccurrentPage,
                    pageSize:this.dicpageSize,
                    keyword: this.filters.keyword,
                    types_id:row.id
                };
                this.$http.post("/detail/pageType",para)
                    .then(result=>{
                        this.listLoading = false;//开启忙等框
                        result=result.data;
                        this.details=result.rows;
                        this.dictotal=result.total;
                    })
                    .catch(result=>{
                        this.$message({
                            type: 'erorr',
                            message: '程序员看小姐姐去了!'
                        });
                    })
            },
            /*del:function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete("/detail/"+row.id)
                        .then(result=>{
                            this.listLoading = false;
                            result=result.data;
                            if(result.success){
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                this.currentPage=1;
                                this.getDetails();
                            }
                            else{
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                        })
                        .catch(result=>{
                            this.$message({
                                message: '系统繁忙,请稍后再试',
                                type: 'error'
                            });
                        })
                }).catch(() => {
                    this.$message({
                        type: 'error',
                        message: '程序员看小姐姐去了!'
                    });
                });
            },*/
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDetailTypes();
			},
			dichandleCurrentChange(val) {
				this.diccurrentPage = val;
				this.getDetails(this.row);
			},
			search(){
			    this.currentPage=1;
			    this.getDetailTypes();
			},
			//获取明细类型列表
			getDetailTypes() {
				let para = {
					currentPage: this.currentPage,
					pageSize:this.pageSize,
                    keyword: this.filters.keyword
				};
				this.listLoading = true;
				//NProgress.start();
				//发送axios请求
				//alert(this.$http)
				this.$http.post("/dic/page",para)
					.then(result=>{
                        this.listLoading = false;//开启忙等框
					    result=result.data;
					    console.log(result)
					    this.detailTypes=result.rows;
					    this.total=result.total;
					})
					.catch(result=>{
                        this.$message({
                            type: 'erorr',
                            message: '程序员看小姐姐去了!'
                        });
					})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/dic/"+row.id)
						.then(result=>{
                            this.listLoading = false;
                            result=result.data;
						    if(result.success){
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                this.currentPage=1;
                                this.getDetailTypes();
							}
						    else{
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
							}
						})
						.catch(result=>{
                            this.$message({
                                message: '系统繁忙,请稍后再试',
                                type: 'error'
                            });
						})
				}).catch(() => {
                    this.$message({
                        type: 'error',
                        message: '程序员看小姐姐去了!'
                    });
				});
			},
            //显示编辑界面
            handleEdit: function (index, row) {
                this.title = "编辑";
                this.saveFormVisible = true;
                this.saveForm = Object.assign({}, row);//克隆一份数据用来做回显
				console.log(row)
                this.$refs['saveForm'].resetFields();//重置表单校验
            },
			//显示新增界面
			handleAdd: function () {
                this.title = "新增";
				this.saveFormVisible = true;
				this.saveForm = {
                    id:null,
                    name: "",
                    sn:""
				};
                this.$refs['saveForm'].resetFields();//重置表单校验
			},
			//显示抽屉
            handledrawer:function(row){
			    this.title=row.name;
                this.getDetails(row);
				this.drawer = true;
			},
			//新增和修改
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;
							let para = Object.assign({}, this.saveForm);
							this.$http.post("/dic/save",para)
								.then(result=>{
								    if(result.data.success){
                                        this.saveLoading = false;
                                        this.$message({
                                            message: result.data.message,
                                            type: 'success'
                                        });
                                        this.currentPage = 1;
                                        this.getDetailTypes();
                                        this.$refs['saveForm'].resetFields();//重置表单校验
                                        this.saveFormVisible = false;//关闭模态框
									}else {
                                        this.$message({
                                            type: 'error',
                                            message: result.data.message
                                        });
                                        this.currentPage = 1;
                                        this.getDetailTypes();
									}
								})
								.catch(result=>{
                                    this.saveLoading = false;//关闭按钮忙等
                                    this.$message({
                                        type: 'error',
                                        message: '程序员跑啦.系统出错啦'
                                    });
								})
						});
					}
				});
			},
		},
        handleDel: function (index, row) {
            this.$confirm('确认删除该记录吗?', '提示', {
                type: 'warning'
            }).then(() => {
                this.listLoading = true;
                this.$http.delete("/detail/"+row.id)
                    .then(result=>{
                        this.listLoading = false;
                        result=result.data;
                        if(result.success){
                            this.$message({
                                message: result.message,
                                type: 'success'
                            });
                            this.currentPage=1;
                            this.getDetails();
                        }
                        else{
                            this.$message({
                                message: result.message,
                                type: 'error'
                            });
                        }
                    })
                    .catch(result=>{
                        this.$message({
                            message: '系统繁忙,请稍后再试',
                            type: 'error'
                        });
                    })
            }).catch(() => {
                this.$message({
                    type: 'error',
                    message: '程序员看小姐姐去了!'
                });
            });

        },
		mounted() {
			this.getDetailTypes();
		}
	}

</script>

<style scoped>

</style>